import UIKit

// 登录窗口回调
protocol LogonWindowCallback: NSObjectProtocol {
    func onLogonWindowOK(_ username: String, password: String)  // 用户单击"确定"按钮事件
}

// 登录窗口
class LogonWindow: NSObject, UITextFieldDelegate {
    private let mAlertController: UIAlertController // 对话框控制者
    private let mViewController: UIViewController   // 视图控制者

    /**
     * 构造方法。
     * @param callback          回调
     * @param viewController    视图控制者
     */
    public init(_ callback: LogonWindowCallback, viewController: UIViewController) {
        // 初始化成员
        var title = NSLocalizedString("IDS_LOGON", tableName: "Main", comment: "")
        mAlertController = UIAlertController(title: title, message: nil, preferredStyle: .alert)
        mViewController = viewController
        super.init()
        
        // 添加"用户名"编辑框
        mAlertController.addTextField(configurationHandler: {
            textField in
            textField.delegate = self
            textField.placeholder = NSLocalizedString("IDS_USERNAME", tableName: "Main", comment: "")
        })
        
        // 添加"密码"编辑框
        mAlertController.addTextField(configurationHandler: {
            textField in
            textField.delegate = self
            textField.placeholder = NSLocalizedString("IDS_PASSWORD", tableName: "Main", comment: "")
            textField.isSecureTextEntry = true
        })
        
        // 添加"取消"按钮
        title = NSLocalizedString("IDS_CANCEL", tableName: "Main", comment: "")
        let buttonCancel = UIAlertAction(title: title, style: .default)
        mAlertController.addAction(buttonCancel)
        
        // 添加"确定"按钮
        title = NSLocalizedString("IDS_OK", tableName: "Main", comment: "")
        let buttonOK = UIAlertAction(title: title, style: .default, handler: {
            [self] _ in
            let username = mAlertController.textFields![0].text!
            let password = mAlertController.textFields![1].text!
            
            // 检查用户名是否为空
            if username.isEmpty {
                let tipText = NSLocalizedString("IDS_USERNAME_EMPTY", tableName: "Main", comment: "")
                Toast.makeText(mViewController.view, text: tipText, duration: Toast.LENGTH_LONG).show()
                LogonWindow(callback, viewController: mViewController).show()
                return
            }
            
            // 检查密码是否为空
            if password.isEmpty {
                let tipText = NSLocalizedString("IDS_PASSWORD_EMPTY", tableName: "Main", comment: "")
                Toast.makeText(mViewController.view, text: tipText, duration: Toast.LENGTH_LONG).show()
                LogonWindow(callback, viewController: mViewController).show()
                return
            }
            
            // 调用回调
            callback.onLogonWindowOK(username, password: password)
        })
        mAlertController.addAction(buttonOK)
    }
    
    /**
     * 设置编辑框最大输入长度。
     * @param textField 编辑框
     * @param range     ?
     * @param string    ?
     * @return 不超过最大长度则返回 true，否则返回 false
     */
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let text = textField.text!
        let len = text.count + string.count - range.length
        if textField.tag == Const.TAG_EDIT_USERNAME {
            return len <= Const.MAX_USERNAME_LEN
        }
        return len <= Const.MAX_PASSWORD_LEN
    }
    
    /**
     * 显示。
     */
    public func show() {
        mViewController.present(mAlertController, animated: true)
    }
}
